package com.example.demo.mapper;

import com.example.demo.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserManagementMapper {

     // 分页查询用户
    @Select("""
        <script>
        SELECT * 
        FROM sys_user
        WHERE 1=1
        <if test="nickname != null and nickname != ''">
            AND nickname LIKE CONCAT('%', #{nickname}, '%')
        </if>
        LIMIT #{offset}, #{size}
        </script>
        """)
    List<User> findUsers(@Param("offset") int offset, @Param("size") int size, @Param("nickname") String nickname);

    @Select("SELECT id, username, nickname, type, gender, birthday, address, phone, email, update_time, create_time " +
        "FROM sys_user WHERE nickname LIKE CONCAT('%', #{nickname}, '%')")
    List<User> selectUser(String nickname);

    // 查询用户总数
    @Select("""
        <script>
        SELECT COUNT(*)
        FROM sys_user
        WHERE 1=1
        <if test="nickname != null and nickname != ''">
            AND nickname LIKE CONCAT('%', #{nickname}, '%')
        </if>
        </script>
        """)
    int countUsers(@Param("nickname") String nickname);

    // 新增用户
    @Insert("""
        INSERT INTO sys_user (id, username, password, nickname, type, gender, birthday, address, phone, email, orgId, create_time, update_time)
        VALUES (#{id}, #{username}, #{password}, #{nickname}, #{type}, #{gender}, #{birthday}, #{address}, #{phone}, #{email}, #{orgId}, NOW(), NOW())
        """)
    int addUser(User user);

    // 根据 ID 删除用户
    @Delete("DELETE FROM sys_user WHERE id = #{id}")
    int deleteUser(@Param("id") Long id);

    // 更新用户
    @Update("""
        UPDATE sys_user
        SET username = #{username},
            nickname = #{nickname},
            type = #{type},
            gender = #{gender},
            birthday = #{birthday},
            address = #{address},
            phone = #{phone},
            email = #{email},
            orgId = #{orgId},
            update_time = NOW()
        WHERE id = #{id}
        """)
    int updateUser(User user);
}
